package com.yanqu.road.utils;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


public class TimeHelper {

	private static Logger logger = LogManager.getLogger(TimeHelper.class.getName());

	public static int getCurrentSecond() {
		return getCalendar().get(Calendar.SECOND);
	}

	public static Timestamp getMillisToDate(long value) {
		return new Timestamp(value);
	}

	public static String getStringDate(long value) {
		if(value==0){
			return "";
		}
		SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置格式
		String timeText=format.format(value*1000);
		return timeText;
	}


	/**
	 * 获取默认日期2000-01-01
	 * @return 返回默认起始时间
	 */
	public static Timestamp getDefaultDate() {
		Date defaultDate = null;
		try {
			defaultDate = (Date) new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
					.parseObject("2000-01-01 00:00:00");

		} catch (ParseException e) {
			logger.error("异常错误",e);
		}
		return new Timestamp(defaultDate.getTime());
	}

	/**
	 * 获取当前时间的秒
	 *
	 * @return
	 */
	public static int getSysTimeSeconds() {
		Calendar cal = getCalendar();
		return cal.get(Calendar.HOUR_OF_DAY) * 3600 + cal.get(Calendar.MINUTE) * 60 + cal.get(Calendar.SECOND);
	}


	/**
	 * <pre>
	 * 比较日期是否同一天(注意：分界线为晚上 12 点)
	 * </pre>
	 * @param date
	 * @return
	 */
	public static boolean dateCompare(long date) {
		Calendar now = getCalendar();
		Calendar other = getCalendar(getMillisToDate(date));
		return dateCompare(now, other) == 0 ? true : false;
	}

	/**
	 * 返回两个日期相差天数
	 *
	 * @param startDate
	 *            开始日期
	 * @param endDate
	 *            结束日期
	 * @return
	 */
	public static int dateCompare(Calendar startDate, Calendar endDate) {
		startDate.set(Calendar.HOUR_OF_DAY, 0);
		startDate.set(Calendar.MINUTE, 0);
		startDate.set(Calendar.SECOND, 0);
		startDate.set(Calendar.MILLISECOND, 0);

		endDate.set(Calendar.HOUR_OF_DAY, 0);
		endDate.set(Calendar.MINUTE, 0);
		endDate.set(Calendar.SECOND, 0);
		endDate.set(Calendar.MILLISECOND, 0);

		int day = (int) (endDate.getTimeInMillis() / 1000 / 60 / 60 / 24
				- startDate.getTimeInMillis() / 1000 / 60 / 60 / 24);
		return day;
	}

	/**
	 * <pre>
	 * 比较日期是否同一天(注意：分界线为晚上 12 点)
	 * </pre>
	 *
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws Exception
	 */
	public static int dateCompare(Date startDate, Date endDate) {
		if (startDate == null || endDate == null) {
			logger.error("日期比较出现异常 数值为 null");
			return 0;
		}
		Calendar c1 = getCalendar(startDate);
		Calendar c2 = getCalendar(endDate);
		return dateCompare(c1, c2);
	}



	/**
	 * 获取系统时间
	 *
	 * @return
	 */
	public synchronized static Calendar getCalendar() {
		return Calendar.getInstance();
	}

	/**
	 * 获取指定的时间
	 *
	 * @param date
	 * @return
	 */
	public static Calendar getCalendar(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		return calendar;
	}

	public static Date addTime(Date current, int type, int value) {
		Calendar cal = getCalendar(current);
		switch (type) {
		case Calendar.DATE:// 增加天数
			cal.add(Calendar.DATE, value);
			break;
		case Calendar.HOUR:// 增加小时
			cal.add(Calendar.HOUR, value);
			break;
		case Calendar.MINUTE:// 增加分钟
			cal.add(Calendar.MINUTE, value);
			break;
		case Calendar.SECOND:// 增加秒
			cal.add(Calendar.SECOND, value);
			break;
		case Calendar.MILLISECOND:// 增加毫秒
			cal.add(Calendar.MILLISECOND, value);
			break;
		default:
			break;
		}
		return new Date(cal.getTimeInMillis());
	}


	public static final SimpleDateFormat getSimpleDateFormat(final String pattern) {
		ThreadLocal<SimpleDateFormat> sdf = sdfMap.get(pattern);
		if (sdf == null) {
			sdf = new ThreadLocal<SimpleDateFormat>() {
				@Override
				protected SimpleDateFormat initialValue() {
					return new SimpleDateFormat(pattern);
				}
			};
			sdfMap.put(pattern, sdf);
		}
		return sdf.get();
	}




	private static final Map<String, ThreadLocal<SimpleDateFormat>> sdfMap = new HashMap<String, ThreadLocal<SimpleDateFormat>>();



	/**
	 * 格式化日期
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String getDateFormat(Date date, String format) {
		SimpleDateFormat dateFm = getSimpleDateFormat(format);
		if (date == null)
			date = getDefaultDate();
		return dateFm.format(date);
	}



	/**
	 * 获取系统距1970年1月1日总毫秒
	 * @return
	 */
	// TODO sk(done) 删除这个方法性能太差
	public static long getSysCurTimeMillis() {
		return getCalendar().getTimeInMillis();
	}

	/**
	 * 获取系统距1970年1月1日总秒
	 * @return
	 */
	public static long getSysCurSeconds() {
		return getCalendar().getTimeInMillis() / 1000;
	}

	/**
	 * 获取系统当前时间
	 * @return
	 */
	public static Timestamp getSysteCurTime() {
		Timestamp ts = new Timestamp(getCalendar().getTimeInMillis());
		return ts;
	}

	/**
	 * 现在是否位于两个时间点中间
	 *
	 * @param startDate
	 *            开始日期
	 * @param endDate
	 *            结束日期
	 * @return
	 */
	public static boolean betweenDate(Calendar startDate, Calendar endDate) {
		Calendar now = getCalendar();
		return now.compareTo(endDate) <= 0 && now.compareTo(startDate) >= 0;
	}
}
